<?php
// +----------------------------------------------------------------------
// | 用户画像
// +----------------------------------------------------------------------
namespace app\passport\controller;
use app\passport\model\Face as FaceModel;


class Face extends Base {

    private $FaceModel;

    public function _initialize() {
        parent::_initialize();
        $this->FaceModel = new FaceModel;
    }

     /**
     * 列表
     * @access public
     */
    public function index(){
        $this->isAuth('passport.face.index');
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_face_ui_title'),'url'=>'javascript:;']
        ];
        $editMenu = [
            ['name'=>'<i class="iconfont icon-cjsqm"></i> '.lang('lang_ui_add'),'url'=>url('passport/face/add')]
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $info = $this->FaceModel->select_lists();
        $page = $info->render();
        $this->assign('lists',$info);
        $this->assign('page',$page);
        return $this->fetch(); 
    }


    /**
     * 画像判断
     * @access public
     */
    public function lists(){
        $this->isAuth('passport.face.lists');
        $id = input('id/d');
        switch ($id) {
            case 1:
                $this->redirect(url('passport/face/face_student'));
                break;
        }
    }

    /**
     * 画像列表
     * @access public
     */
    public function face_student(){
        $this->isAuth('passport.face.lists');
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>'javascript:history.go(-1)']
        ];
        $editMenu = [];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $where['name']  = input('name/s');
        $where['work']  = input('work/s');
        $where['phone'] = input('phone/s');
        $info = $this->FaceModel->face_student($where);
        $counts = $this->FaceModel->face_student_counts($where);
        $page = $info->render();
        $this->assign('lists',$info);
        $this->assign('counts',$counts);
        $this->assign('page',$page);
        $this->assign('where',$where);
        return $this->fetch(); 
    }


    /**
     * 添加
     * @access public
     */
    public function add() {
        $this->isAuth('passport.face.add');
        if(request()->isAjax()){
            $data = [
                'face_name'  => input('post.face_name/s'),
                'table_name' => input('post.table_name/s'),
                'about'      => input('post.about/s'),
            ];
            $validate = $this->validate($data,'Face.add');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->FaceModel->add_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('passport/face/index')]]);
            }
        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('passport/face/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            return $this->fetch();
        }
    } 
    
    /**
     * 编辑权限节点
     * @access public
     */
    public function edit() {
        $this->isAuth('passport.face.edit');
        if(request()->isAjax()){
            $data = [
                'id'        => input('post.id/d'),
                'face_name' => input('post.face_name/s'),
                'about'     => input('post.about/s'),
            ];
            $validate = $this->validate($data,'Face.edit');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->FaceModel->edit_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('passport/face/index')]]);
            }
        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('passport/face/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $id   = input('id/d');
            $info = $this->FaceModel->find_info($id);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
}